#!/usr/bin/env bash

export DNS_ENV_HOME=$HOME/.zsh-dns

__dns_export_env() {
  if [ -f "${DNS_ENV_HOME}/dns.env" ]; then
    export $(xargs <${DNS_ENV_HOME}/dns.env)
  fi
}

__dns_env_get() {
  eval echo '$'$(echo DNS_$1_$2_$3 | tr 'a-z' 'A-Z')
}

__dns_env_echo() {
  local n=$1
  local v=$2
  if [ -n "$v" ]; then
    echo "$n $v"
  fi
}

__dns_save() {
  local n=$1
  local v=$2
  if [ -n ${n} ] && [ -n "$v" ]; then
    if [ "$v" = "unset" ]; then
      rm -f "${DNS_ENV_HOME}/${n}"
    else
      mkdir -p "${DNS_ENV_HOME}"
      echo $v >"${DNS_ENV_HOME}/${n}"
    fi
  fi
}

__dns_load() {
  cat ${DNS_ENV_HOME}/$1 2>/dev/null | head -n 1
}

# dnspod
__dns_set_dnspod() {
  local n=$(__dns_load dnspod)
  if [ -z "$n" ] || [ "$n" = "unset" ]; then
    __dns_unset_dnspod
  else
    export DP_Label=$(__dns_env_get dnspod $n label)
    export DP_Id=$(__dns_env_get dnspod $n key)
    export DP_Key=$(__dns_env_get dnspod $n secret)
    export DNSPOD_API_KEY="${DP_Id},${DP_Key}"
  fi
}

__dns_unset_dnspod() {
  unset DP_Label DP_Id DP_Key DNSPOD_API_KEY
}

__dns_display_dnspod() {
  __dns_env_echo "名称:" "${DP_Label}"
  __dns_env_echo "标识:" "${DP_Id}"
  __dns_env_echo "秘钥:" "${DP_Key}"
}

# ali
__dns_set_ali() {
  local n=$(__dns_load ali)
  if [ -z "$n" ] || [ "$n" = "unset" ]; then
    __dns_unset_ali
  else
    export Ali_Label=$(__dns_env_get ali $n label)
    export Ali_Key=$(__dns_env_get ali $n key)
    export Ali_Secret=$(__dns_env_get ali $n secret)
    export ALICLOUD_ACCESS_KEY=${Ali_Key}
    export ALICLOUD_SECRET_KEY=${Ali_Secret}
  fi
}

__dns_unset_ali() {
  unset Ali_Label Ali_Key Ali_Secret ALICLOUD_ACCESS_KEY ALICLOUD_SECRET_KEY
}

__dns_display_ali() {
  __dns_env_echo "名称:" "${Ali_Label}"
  __dns_env_echo "标识:" "${Ali_Key}"
  __dns_env_echo "秘钥:" "${Ali_Secret}"
}

# godaddy
__dns_set_godaddy() {
  local n=$(__dns_load godaddy)
  if [ -z "$n" ] || [ "$n" = "unset" ]; then
    __dns_unset_godaddy
  else
    export GD_Label=$(__dns_env_get godaddy $n label)
    export GD_Key=$(__dns_env_get godaddy $n key)
    export GD_Secret=$(__dns_env_get godaddy $n secret)
    export GD_ShopId=$(__dns_env_get godaddy $n shopid)
  fi
}

__dns_unset_godaddy() {
  unset GD_Label GD_Key GD_Secret GD_ShopId
}

__dns_display_godaddy() {
  __dns_env_echo "名称:" "${GD_Label}"
  __dns_env_echo "标识:" "${GD_Key}"
  __dns_env_echo "秘钥:" "${GD_Secret}"
}

# functions
dns.dnspod() {
  __dns_save dnspod $1
  __dns_set_dnspod
  __dns_display_dnspod
}

alias dns.dnspod.wen='dns.dnspod wen'
alias dns.dnspod.dys='dns.dnspod dys'
alias dns.dnspod.zhao='dns.dnspod zhao'
alias dns.dnspod.unset='dns.dnspod unset'

dns.ali() {
  __dns_save ali $1
  __dns_set_ali
  __dns_display_ali
}

alias dns.ali.wen='dns.ali wen'
alias dns.ali.yw='dns.ali yw'
alias dns.ali.juku='dns.ali juku'
alias dns.ali.unset='dns.ali unset'

dns.godaddy() {
  __dns_save godaddy $1
  __dns_set_godaddy
  __dns_display_godaddy
}

alias dns.godaddy.wen='dns.godaddy wen'
alias dns.godaddy.yw='dns.godaddy yw'
alias dns.godaddy.unset='dns.godaddy unset'

# auto_load
__dns_auto_load() {
  __dns_export_env
  __dns_set_dnspod
  __dns_set_ali
  __dns_set_godaddy
}

__dns_auto_load
